<?php
//by ajsong 172403414@qq.com 2015/06/29

class other extends core {
	public function __construct() {
		parent::__construct();
	}

	//配置开关
	//on1216: 12月16日提交给苹果的审核参数
	//WX_APPID WX_MCHID WX_APPSECRET WX_PARTNERID
	public function c() {
		$configs = array(
			"on0308"=>0
		);
		success($configs);
	}

	//获取开机广告图
	public function launch_image() {
		$sql = "SELECT pic FROM {$this->tbp}launch_image ORDER BY id ASC";
		$result = $this->db->get_results($sql);
		success($result);
	}

	//RSA证书生成
	public function rsa() {
		global $tbp;
		new Rsa(FRAMEWORK_PATH . '/class/rsa/keys', "{$tbp}private", "{$tbp}public");
		exit('Certificate created success');
	}
	
	//设置经纬度
	public function set_geo() {
		$longitude = (isset($_GET['longitude']) && trim($_GET['longitude'])) ? floatval($_GET['longitude']) : 0;
		$latitude = (isset($_GET['latitude']) && trim($_GET['latitude'])) ? floatval($_GET['latitude']) : 0;
		$_SESSION['longitude'] = $longitude;
		$_SESSION['latitude'] = $latitude;
		success('ok');
	}
	
	//获取地址的经纬度
	public function get_geo($address='', $is_show=true) {
		if (!$address) $address = (isset($_GET['address']) && trim($_GET['address'])) ? trim($_GET['address']) : '';
		$url = "http://api.map.baidu.com/geocoder/v2/?ak={$this->baidu_ak}&address={$address}&output=json";
		$html = file_get_contents($url);
		$json = json_decode($html);
		if (!isset($json->status)) error('BAIDU API ERROR');
		if (intval($json->status)!=0) error($json->message);
		$data = array('longitude'=>floatval($json->result->location->lng), 'latitude'=>floatval($json->result->location->lat));
		if ($is_show) {
			success($data);
		} else {
			return $data;
		}
	}
	
	//手机经纬度转百度坐标
	public function get_baidu_geo() {
		$longitude = (isset($_GET['longitude']) && trim($_GET['longitude'])) ? floatval($_GET['longitude']) : 0;
		$latitude = (isset($_GET['latitude']) && trim($_GET['latitude'])) ? floatval($_GET['latitude']) : 0;
		$url = "http://api.map.baidu.com/geoconv/v1/?ak={$this->baidu_ak}&coords={$longitude},{$latitude}";
		$html = file_get_contents($url);
		$json = json_decode($html);
		if (!isset($json->status)) error('BAIDU API ERROR');
		if (intval($json->status)!=0) error($json->message);
		$data = array(
			'longitude'=>$json->result[0]->x,
			'latitude'=>$json->result[0]->y
		);
		success($data);
	}
	
	//手机经纬度获取地址
	public function get_geo_address() {
		$longitude = (isset($_GET['longitude']) && trim($_GET['longitude'])) ? floatval($_GET['longitude']) : 0;
		$latitude = (isset($_GET['latitude']) && trim($_GET['latitude'])) ? floatval($_GET['latitude']) : 0;
		$url = "http://api.map.baidu.com/geocoder/v2/?ak={$this->baidu_ak}&location={$latitude},{$longitude}&coordtype=wgs84ll&output=json";
		$html = file_get_contents($url);
		$json = json_decode($html);
		if (!isset($json->status)) error('BAIDU API ERROR');
		if (intval($json->status)!=0) error($json->message);
		$data = array(
			'country'=>$json->result->addressComponent->country,
			'country_code_iso'=>$json->result->addressComponent->country_code_iso,
			'province'=>$json->result->addressComponent->province,
			'city'=>$json->result->addressComponent->city,
			'district'=>$json->result->addressComponent->district,
			'adcode'=>$json->result->addressComponent->adcode,
			'street'=>$json->result->addressComponent->street,
			'street_number'=>$json->result->addressComponent->street_number,
			'formatted_address'=>$json->result->formatted_address,
			'sematic_description'=>$json->result->sematic_description
		);
		success($data);
	}
	
	//获取两个经纬度之间的距离,单位公里
	public function get_distance($geo=array(), $is_show=true) {
		if (count($geo)) $_GET = $geo;
		$lng1 = (isset($_GET['lng1']) && trim($_GET['lng1'])) ? floatval($_GET['lng1']) : 0;
		$lat1 = (isset($_GET['lat1']) && trim($_GET['lat1'])) ? floatval($_GET['lat1']) : 0;
		$lng2 = (isset($_GET['lng2']) && trim($_GET['lng2'])) ? floatval($_GET['lng2']) : 0;
		$lat2 = (isset($_GET['lat2']) && trim($_GET['lat2'])) ? floatval($_GET['lat2']) : 0;
		$sql = "SELECT fun_distance({$lat1}, {$lng1}, {$lat2}, {$lng2})";
		$distance = floatval($this->db->get_var($sql));
		if ($is_show) {
			success($distance);
		} else {
			return $distance;
		}
	}
	
	//获取两个地址之间的距离,单位公里
	public function get_distance_from_address($address1='', $address2='', $is_show=true) {
		if (!$address1) $address1 = (isset($_GET['address1']) && trim($_GET['address1'])) ? trim($_GET['address1']) : '';
		if (!$address2) $address2 = (isset($_GET['address2']) && trim($_GET['address2'])) ? trim($_GET['address2']) : '';
		$data1 = $this->get_geo($address1, false);
		$data2 = $this->get_geo($address2, false);
		$distance = $this->get_distance(array(
			'lng1'=>$data1['longitude'], 'lat1'=>$data1['latitude'], 'lng2'=>$data2['longitude'], 'lat2'=>$data2['latitude']
		), false);
		if ($is_show) {
			success($distance);
		} else {
			return $distance;
		}
	}

	public function handle_meal() {
		$meal_ids = $op_ids = "";
		$sql = "select m.id as mid, o.id as order_id, p.id as opid, o.add_time
			from {$this->tbp}member_meal m INNER JOIN im_order o ON m.member_id=o.member_id
			INNER JOIN {$this->tbp}order_project p ON o.id=p.order_id
			where m.meal_id=10 and m.num=1 and o.total_price=0 and o.status>=4
			and p.project_id=22 AND p.meal_id=0";
		$meals = $this->db->get_results($sql);
		if ($meals) {
			foreach ($meals as $key => $row) {
				if ($meal_ids) {
					$meal_ids .= ", {$row->mid}";
				} else {
					$meal_ids = $row->mid;
				}
				if ($op_ids) {
					$op_ids .= ", {$row->opid}";
				} else {
					$op_ids = $row->opid;
				}
			}
			$sql = "UPDATE {$this->tbp}order_project SET meal_id='10' WHERE id IN($op_ids) AND meal_id=0";
			echo $sql;
			$this->db->query($sql);
			//exit;
			$sql = "UPDATE {$this->tbp}member_meal SET num=0 WHERE id IN ({$meal_ids})";
			echo $sql;
			$this->db->query($sql);
		}
	}

	public function project_ids() {
		$rs = $this->db->get_results("select id from {$this->tbp}order");
		foreach ($rs as $order) {
			$projects = $this->db->get_results("select project_id from {$this->tbp}order_project where order_id='{$order->id}'");
			if ($projects) {
				$ids = "";
				foreach ($projects as $k => $p) {
					$ids .= $p->project_id.",";
				}
				if ($ids) $ids = ",{$ids}";
				$sql = "update {$this->tbp}order set project_ids='{$ids}' where id='{$order->id}'";
				echo $sql."<br>";
				$this->db->query($sql);
			}
		}
	}

	public function info() {
		//phpinfo();
	}
	
	//微信小程序工具
	public function wxtool() {
		require_once ROOT_PATH . '/test/mobile_unit.html';
	}
	//移动单位转换
	public function image_base64() {
		if (!WX_MINIPROGRAM) exit('Program is disabled.');
		$file = isset($_FILES['file']) ? $_FILES['file'] : '';
		if (!$file) exit;
		echo image_base64($file['tmp_name']);
	}
	public function image_online() {
		if (!WX_MINIPROGRAM) exit('Program is disabled.');
		$url = upload_one_file('pic', 'file');
		echo $url;
	}

	//物流查询
	//快递鸟接口
	public function kdniao() {
		$spell_name = (isset($_GET['spell_name']) && trim($_GET['spell_name'])) ? trim($_GET['spell_name']) : '';
		$mail_no = (isset($_GET['mail_no']) && trim($_GET['mail_no'])) ? trim($_GET['mail_no']) : '';
		if (!$spell_name || !$mail_no) error('数据错误');
		$result = getkdniao($spell_name, $mail_no);
		success($result);
	}
	//爱查快递接口
	public function ickd() {
		$spell_name = (isset($_GET['spell_name']) && trim($_GET['spell_name'])) ? trim($_GET['spell_name']) : '';
		$mail_no = (isset($_GET['mail_no']) && trim($_GET['mail_no'])) ? trim($_GET['mail_no']) : '';
		if (!$spell_name || !$mail_no) error('数据错误');
		$result = getickd($spell_name, $mail_no);
		success($result);
	}

	//给所有旧用户都发一张68元
	public function send_coupon() {
		$sql = "SELECT * FROM {$this->tbp}member";
		$members = $this->db->get_results($sql);
		foreach ($members as $k => $member) {
			$sql = "SELECT count(*) FROM {$this->tbp}member_cash_coupon WHERE member_id='{$member->id}' AND cash_coupon_id=1";
			if (intval($this->db->get_var($sql))==0) {
				$sql = "INSERT INTO {$this->tbp}member_cash_coupon(member_id, cash_coupon_id, cash_coupon_name, face_value,
					expiring_time, add_time, status, used_time)
					VALUES('{$member->id}', '1', '现金券', '68', '1454255999', '1451544112', '1', '0')";
				echo $sql."<br>";
				$this->db->query($sql);
			}
		}
	}

	public function import_member() {
		require_once FRAMEWORK_PATH . '/class/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
		$objPHPExcel = PHPExcel_IOFactory::load(ROOT_PATH."/temp/member.xlsx");
		//echo date('H:i:s') ;
		//$objWorksheet = $objPHPExcel->getActiveSheet();
		 $sheets = $objPHPExcel->setActiveSheetIndex(0);
		 foreach ($objPHPExcel->getWorksheetIterator() as $objWorksheet) {
			//获取行数
			$highestRow = $objWorksheet->getHighestRow();
			//获取列数
			$highestColumn = $objWorksheet->getHighestColumn();
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$data = array();
			for($row = 1; $row <= $highestRow; ++$row) {
				for($col = 0; $col < $highestColumnIndex; ++$col) {
					$val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					$data[$row][$col] = trim($val);
				}
			}

			foreach ($data as $k => $row) {
				if ($k>=2) {
					//写入会员
					$fenyuan = trim($row[0]);
					$kahao = trim($row[1]);
					$name = trim($row[2]);
					$tel = trim($row[3]);
					$mobile = trim($row[4]);
					if ($tel=="" && $mobile=="") {
						echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话为空<br></p>";
					} else {
						if ($mobile=="") {
							$mobile = $tel;
						}
						if ($mobile && is_numeric($mobile)) {
							$sql = "SELECT count(*) FROM {$this->tbp}member WHERE mobile='{$mobile}'";
							if ($this->db->get_var($sql)) {
								echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话已经存在<br></p>";
							} else {
								$sql = "INSERT INTO {$this->tbp}member(name, mobile, reg_time, nick_name, fenyuan, kahao)
									VALUES('{$name}', '{$mobile}', '".time()."', '{$name}', '{$fenyuan}', '{$kahao}')";
								$this->db->query($sql);
								echo $sql."<br>";
							}
						} else {
							echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话为空<br></p>";
						}
						//exit;
					}
				}
			}
		}
	}

	public function import_jiameng() {
		require_once FRAMEWORK_PATH . '/class/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
		$objPHPExcel = PHPExcel_IOFactory::load(ROOT_PATH."/temp/jiameng.xlsx");
		//echo date('H:i:s') ;
		//$objWorksheet = $objPHPExcel->getActiveSheet();
		 $sheets = $objPHPExcel->setActiveSheetIndex(0);
		 foreach ($objPHPExcel->getWorksheetIterator() as $objWorksheet) {
			//获取行数
			$highestRow = $objWorksheet->getHighestRow();
			//获取列数
			$highestColumn = $objWorksheet->getHighestColumn();
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$data = array();
			for($row = 1; $row <= $highestRow; ++$row) {
				for($col = 0; $col < $highestColumnIndex; ++$col) {
					$val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					$data[$row][$col] = trim($val);
				}
			}

			foreach ($data as $k => $row) {
				if ($k>=1) {
					//写入会员
					$fenyuan = trim($row[2]);
					$name = trim($row[0]);
					$mobile = trim($row[1]);
					if ($mobile=="") {
						echo "<p style='color:red'>{$fenyuan} {$name}电话为空<br></p>";
					} else {
						if ($mobile && is_numeric($mobile)) {
							$sql = "SELECT count(*) FROM {$this->tbp}member WHERE mobile='{$mobile}'";
							if ($this->db->get_var($sql)) {
								$sql = "UPDATE {$this->tbp}member SET member_type='3' WHERE mobile='{$mobile}'";
								$this->db->query($sql);
								echo "<p style='color:red'>{$fenyuan} {$name}电话已经存在<br></p>";
							} else {
								$sql = "INSERT INTO {$this->tbp}member(name, mobile, reg_time, nick_name, fenyuan)
									VALUES('{$name}', '{$mobile}', '".time()."', '{$name}', '{$fenyuan}')";
								$this->db->query($sql);
								echo $sql."<br>";
							}
						} else {
							echo "<p style='color:red'>{$fenyuan} {$name}电话为空<br></p>";
						}
						//exit;
					}
				}
			}
		}
	}

	public function add_activity() {
		echo uniqid();
		exit;
		$sql = "SELECT * FROM {$this->tbp}activity_zhuanpan WHERE id='8'";
		$activity = $this->db->get_row($sql);
		for ($id=49; $id<=61; $id++) {
			$sql = "INSERT INTO {$this->tbp}activity_zhuanpan_jiangpin(activity_id, name, description, pic,
				quantity, jiangpin_type, ext_id, people, open_time)
				VALUES('{$id}', '{$activity->name}', '{$activity->description}', '{$activity->pic}',
				'{$activity->quantity}', '{$activity->jiangpin_type}', '{$activity->ext_id}', '{$activity->people}', '{$activity->open_time}')";
			echo $sql."<br>";
			$this->db->query($sql);
		}
	}

	//android检测更新,在后台参数设置处增加 g_update_version、g_update_content、g_update_url
	public function update() {
		$data = null;
		if (isset($this->configs['g_update_version'])) {
			$data = new stdClass();
			$data->version = $this->configs['g_update_version'];
			$data->content = $this->configs['g_update_content'];
			$data->url = $this->configs['g_update_url'];
		}
		success($data);
	}

}
